package com.kawaiior.asoulworld.item.weapon;

import com.kawaiior.asoulworld.item.ItemASWeapon;
import com.kawaiior.asoulworld.potion.ModPotions;
import com.kawaiior.asoulworld.util.EntityUtil;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;


import javax.annotation.Nonnull;

public class ItemCarolFansSword extends ItemASWeapon {
    public ItemCarolFansSword(String name, ToolMaterial material) {
        super(name, material);
        useable = true;
        base_cd = 8f;
        attackDamage = 7f;
//        MinecraftForge.EVENT_BUS.register(this);
    }


    @Override
    public int getMaxDamage(ItemStack stack) {
        return 1024;
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 60;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
        if (useable)
        {
            player.setActiveHand(hand);
            ItemStack stack = player.getHeldItem(hand);
            if (!world.isRemote)
            {
                stack.damageItem(8, player);
                EntityUtil.ApplyBuff(player, ModPotions.COUNTER_ATTACK,0,3.0f);
                player.getCooldownTracker().setCooldown(this, getCoolDownTicks(stack));
            }
            return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
        }
        else {
            return super.onItemRightClick(world, player, hand);
        }
    }

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
    {
        if (entityLiving instanceof EntityPlayer)
        {
            EntityPlayer player = (EntityPlayer)entityLiving;
            player.removePotionEffect(ModPotions.COUNTER_ATTACK);
        }
    }

//    @Override
//    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
//    {
//        if (!useable)
//        {
//            return;
//        }
//
//        if (!worldIn.isRemote)
//        {
//            if (entityLiving instanceof EntityPlayer)
//            {
//                EntityPlayer entityplayer = (EntityPlayer)entityLiving;
//                EntityUtil.ApplyBuff(entityplayer, ModPotions.COUNTER_ATTACK,0,0.8f);
//                stack.damageItem(8, entityLiving);
//                entityplayer.getCooldownTracker().setCooldown(this, getCoolDownTicks(stack));
//            }
//        }
//    }

//    @SubscribeEvent
//    public void playerCounterAttack(LivingHurtEvent event)
//    {
//        World world = event.getEntity().world;
//        if (!world.isRemote)
//        {
//            EntityLivingBase hurtOne = event.getEntityLiving();
//            EntityLivingBase attacker = (EntityLivingBase) event.getSource().getTrueSource();
//            float f = event.getAmount();
//            float h = hurtOne.getHealth();
//            if (attacker !=null && hurtOne.getHeldItemMainhand().getItem() == this && hurtOne.isPotionActive(ModPotions.COUNTER_ATTACK) && hurtOne instanceof EntityPlayer)
//            {
//                EntityUtil.ApplyBuff(hurtOne, MobEffects.RESISTANCE,4,0.5f);
//                EntityUtil.TryRemoveGivenBuff(hurtOne,ModPotions.COUNTER_ATTACK);
//                EntityPlayer player = (EntityPlayer)hurtOne;
//                if (h>f)
//                {
//                    attacker.attackEntityFrom(DamageSource.causeMobDamage(hurtOne),f);
//                    EntityUtil.ApplyBuff(hurtOne, MobEffects.REGENERATION,2,2f);
//                    player.getCooldownTracker().setCooldown(this, 15);
//                }
//                else
//                {
//                    EntityUtil.ApplyBuff(player,MobEffects.SPEED,1,3f);
//                    EntityUtil.ApplyBuff(player,MobEffects.STRENGTH,1,3f);
//                    attacker.attackEntityFrom(DamageSource.causeMobDamage(hurtOne),20);
//                    player.getCooldownTracker().setCooldown(this, 5);
//                    player.setHealth(1f);
//                }
//            }
//        }
//    }

//    @SubscribeEvent(priority = EventPriority.HIGHEST)
//    public void breakDefense(LivingHurtEvent event)
//    {
//        World world = event.getEntity().world;
//        if (!world.isRemote)
//        {
//            EntityLivingBase hurtOne = event.getEntityLiving();
//            EntityLivingBase attacker = (EntityLivingBase) event.getSource().getTrueSource();
//            if (attacker instanceof EntityBewilderedManProPlusMax && hurtOne instanceof EntityPlayer && !hurtOne.isPotionActive(ModPotions.COUNTER_ATTACK) && !hurtOne.isPotionActive(MobEffects.WEAKNESS))
//            {
//                EntityPlayer player = (EntityPlayer)hurtOne;
//                EntityUtil.ApplyBuff(player, MobEffects.WEAKNESS,0,2);
//                player.sendMessage(new TextComponentTranslation("message.asoulworld.player_break_defense",null,null));
//            }
//        }
//    }
}
